<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\GuildHostOp;
use App\Http\Controllers\Controller;
use App\Model\Guild\Guild;
use App\Model\Guild\GuildHost;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Model\WhitePhone;
use App\Util\InfoUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

/**
 * 手机白名单
 */
class WhitePhoneController extends Controller
{
    use ModelForm;

    public function index()
    {
        $this->userRole = Admin::user()->roles[0]['slug'];
        $adminId = Admin::user()->id;
        $this->scrtPhone = in_array($adminId, [7, 10]);
        return Admin::content(function (Content $content) use ($adminId) {
            $content->header('手机号白名单列表');
            // $content->description("Uid:{$this->scrtPhone}");
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('详情');
            $content->description('');
            $content->body($this->form('edit', $id)->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加白名单');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(WhitePhone::class, function (Grid $grid) {
            $self = $this;
            $grid->model()->orderBy('id', 'desc');
            $grid->created_at('添加时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i', strtotime($time));
            });
            $grid->phone('手机号');
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '生效' : '无效';
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
//            if ($this->userRole != 'administrator') {
//                $grid->disableCreateButton();
//            }
            $grid->filter(function ($filter) {
                // $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('phone', '手机号');
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(WhitePhone::class, function (Form $form) use ($mode, $id) {
            $form->text('phone', '手机号')->required();
            $form->ignore(['user_id']);
            $form->saving(function (Form $form) {
                $where = ['phone' => $form->phone, 'flag' => 1];
                if (WhitePhone::where($where)->first()) return InfoUtil::adminErr('该手机已存在于白名单！');
            });
            $form->tools(function (Form\Tools $tools) {
                $tools->disableList();
                // $tools->disableDelete();
                $tools->disableView();
            });
        });
    }
}
